package com.choichumgming.rentcat.web.controller;


import com.choichumgming.rentcat.common.api.CommonResult;
import com.choichumgming.rentcat.model.AmsArticle;
import com.choichumgming.rentcat.service.AmsArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName AmsArticleController
 * @Description : 文章管理Controller
 * @Author : choichumgming
 * @Date : 1/3/2020 11:57
*/
@Controller
@Api(tags = "AmsArticleController", description = "文章管理")
@RequestMapping("/")
public class AmsArticleController {
    @Autowired
    private AmsArticleService amsArticleService;


    @ApiOperation("添加文章")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult add(@RequestBody AmsArticle article) {
        int count = amsArticleService.add(article);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除文章")
    @RequestMapping(value = "/delete/{articleId}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long articleId) {
        int count = amsArticleService.delete(articleId);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改文章")
    @RequestMapping(value = "/update/{articleId}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long articleId, @RequestBody AmsArticle article) {
        int count = amsArticleService.update(articleId, article);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("显示指定用户所有文章")
    @RequestMapping(value = "/list/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult list(@PathVariable("userId") Long userId) {
        List<AmsArticle> articleList = amsArticleService.list(userId);
        return CommonResult.success(articleList);
    }

    @ApiOperation("显示文章")
    @RequestMapping(value = "/item/{articleId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getItem(@PathVariable Long articleId) {
        AmsArticle article = amsArticleService.getItem(articleId);
        return CommonResult.success(article);
    }

    @ApiOperation("显示指定类型文章")
    @RequestMapping(value = "/list/category/{categoryId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getAmsArticleList(@PathVariable("categoryId") Long categoryId) {
        List<AmsArticle> articleList  = amsArticleService.getAmsArticleList(categoryId);
        return CommonResult.success(articleList);
    }

    @ApiOperation("删除选中的文章")
    @RequestMapping(value = "/delete/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deletelist(@RequestBody Long[] articleIdList) {
        List<Long> resultList= new ArrayList<>(Arrays.asList(articleIdList));
        int count = amsArticleService.deleteList(resultList);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
//    @ApiOperation("")
//    @RequestMapping(value = "/list/category/{product_category_id}", method = RequestMethod.GET)
//    @ResponseBody
}
